package com.check;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class JsonCheckUtil {
    public static void main(String[] args) throws IOException, ProcessingException {
        JsonCheckUtil.checkJson(
                new File("D:\\demo\\black\\jcq\\device_api\\manager-api-server\\src\\main\\resources\\jsonSchema\\alarm\\AlarmTrojanReq.exam.json"),
                new File("D:\\demo\\black\\jcq\\device_api\\manager-api-server\\src\\main\\resources\\jsonSchema\\alarm\\AlarmTrojanReq.schema.json"));
    }
    /**
     * 检查json合法性
     */
    public static ProcessingReport checkJson(File fromJson,File schemaJson) throws IOException, ProcessingException {
        //要校验的json字符串（如来自自动化测试中的响应结果）
        String jsonStr = FileUtils.readFileToString(fromJson, "UTF-8");
        return getProcessingMessages(schemaJson, jsonStr);
    }

    public static ProcessingReport checkJsonStr(String fromJson,File schemaJson) throws IOException, ProcessingException {
        //要校验的json字符串（如来自自动化测试中的响应结果）
        return getProcessingMessages(schemaJson, fromJson);
    }


    public static ProcessingReport checkJsonInputStream(String fromJson, InputStream schemaJsonInput) throws IOException, ProcessingException {
        //要校验的json字符串（如来自自动化测试中的响应结果）
        String schemaJson = IOUtils.toString(schemaJsonInput, "UTF-8");
        return getProcessingMessages(schemaJson, fromJson);
    }


    public static ProcessingReport getProcessingMessages(String schemaJson, String jsonStr) throws IOException, ProcessingException {
        //根据响应结果需求，编写的JSON Schema
        //创建JsonSchema工厂
        ProcessingReport processingReport = getReport(jsonStr, schemaJson);
        return processingReport;
    }

    private static ProcessingReport getProcessingMessages(File schemaJson, String jsonStr) throws IOException, ProcessingException {
        //根据响应结果需求，编写的JSON Schema
        String jsonSchema = FileUtils.readFileToString(schemaJson, "UTF-8");
        ProcessingReport processingReport = getReport(jsonStr, jsonSchema);
        return processingReport;
    }

    private static ProcessingReport getReport(String jsonStr, String jsonSchema) throws IOException, ProcessingException {
        //创建JsonSchema工厂
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        //通过jsonSchema获得对应JsonNode对象
        JsonNode schemaNode = JsonLoader.fromString(jsonSchema);
        //通过jsonStr字符串获得对应JsonNode对象
        JsonNode dataNode = JsonLoader.fromString(jsonStr);
        //通过jsonSchema的JsonNode对象获得JsonSchema对象
        JsonSchema schema = factory.getJsonSchema(schemaNode);
        //使用json-schema-validator中JsonSchema对象的validate方法对数据进行校验
        //获得处理的报告信息
        ProcessingReport processingReport = schema.validate(dataNode);


        return processingReport;
    }


}
